<?php
class Vs_Question_Mapper
    extends Vs_Mapper
	
{
	public function getTop()
	{
		return $this->_fetchCollection(
				$this->getTable()->getTop()
		);
	}
	
	public function loadCategory( $question )
	{
		$mapper = Vs_Factory::mapper('Category');
	
		if (  $question instanceof Vs_Question_Collection ){
	
			if (count( $question ) < 1){
				return;
			}
				
			$categories = $mapper->getByIds( $question->getKeyValues( 'categoryId', true ) );
			$question->setCategory($categories);
				
		}
		elseif ( $question instanceof VS_Question){
			$question->setCategory(
					$mapper->getById( $question->categoryId )
			);
		}
	}
	
	public function loadCity( $question )
	{
		$mapper = Vs_Factory::mapper('City');
	
		if (  $question instanceof Vs_Question_Collection ){
	
			if (count( $question ) < 1){
				return;
			}
				
			$cityIds = $question->getKeyValues( 'cityId', true, true );
			
			if(!empty($cityIds)){
				$cities = $mapper->getByIds( $question->getKeyValues( 'cityId', true ) );
				$question->setCity($cities);
			}
				
		}
		elseif ( $question instanceof VS_Question && $question->cityId != null ){
			$question->setCity(
					$mapper->getById( $question->cityId )
			);
		}
	}
	
	public function loadReasonFamily( $question )
	{
		$mapper = Vs_Factory::mapper('ReasonFamily');
	
		if (  $question instanceof Vs_Question_Collection ){
	
			if (count( $question ) < 1){
				return;
			}
				
			$reasonFamilyIds = $question->getKeyValues( 'reasonFamilyId', true, true );
			
			if(!empty($reasonFamilyIds)){
				$reasonFamilies = $mapper->getByIds( $reasonFamilyIds );
				$question->setReasonFamily($reasonFamilies);
			}
		}
		elseif ( $question instanceof VS_Question && $question->reasonFamilyId != null){
			$question->setReasonFamily(
					$mapper->getById( $question->reasonFamilyId )
			);
		}
	}
	
	
	public function loadAnswers( $question )
	{
		$mapper = Vs_Factory::mapper('Answer');
	
		if ( $question instanceof Vs_Question_Collection){
	
			if (count( $question ) < 1){
				return;
			}
				
			$answers = $mapper->getByParams( array('questionId' => $question->getIds()) );
	
			$question->setAnswers($answers);
		}
	
		elseif ( $question instanceof Vs_Question){
	
			$answers = $mapper->getByParams( array('questionId' => $question->getId()) );
	
			$question->setAnswers($answers);
		}
	}

}
